﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfToolkit
{
    /// <summary>
    /// MyControl.xaml 的交互逻辑
    /// </summary>
    public partial class MyControl : UserControl
    {
        public string MaxLength
        {
            get { return (string)GetValue(MaxLengthProperty); }
            set { SetValue(MaxLengthProperty, value); }
        }

        public static readonly DependencyProperty MaxLengthProperty =
            DependencyProperty.Register("MaxLength", typeof(string), typeof(MyControl), new PropertyMetadata(string.Empty));

       

        public string Pattern
        {
            get { return (string)GetValue(PatternProperty); }
            set { SetValue(PatternProperty, value); }
        }

        public static readonly DependencyProperty PatternProperty =
            DependencyProperty.Register("Pattern", typeof(string), typeof(MyControl), new PropertyMetadata(string.Empty, ChangePattern));

        private static void ChangePattern(DependencyObject obj, DependencyPropertyChangedEventArgs r)
        {
            MyControl control = (MyControl)obj;
            control.txt1.Text= control.Pattern;
        }
        private static void TextChange(DependencyObject d, DependencyPropertyChangedEventArgs r)
        {
            MyControl control = d as MyControl;
            if (control !=null)
            {
                string oldText = r.OldValue as string;
                string newText = r.NewValue as string;
                control.txt1.Text = newText;
            }
        }
        #region commond
        public static readonly DependencyProperty CommandProperty =
            DependencyProperty.Register(
                    "Command",
                    typeof(ICommand),
                    typeof(MyControl),
                    new FrameworkPropertyMetadata((ICommand)null,new PropertyChangedCallback(CommandChanged)));
        public static readonly DependencyProperty CommandParameterProperty =
              DependencyProperty.Register(
                      "CommandParameter",
                      typeof(object),
                      typeof(MyControl),
                      new FrameworkPropertyMetadata((object)null));
        public static readonly DependencyProperty CommandTargetProperty =
            DependencyProperty.Register(
                    "CommandTarget",
                    typeof(IInputElement),
                    typeof(MyControl),
                    new FrameworkPropertyMetadata((IInputElement)null));

        private static void CommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MyControl control = d as MyControl;
            if (control != null)
            {
                ICommand oldCommand = e.OldValue as ICommand;
                ICommand newCommand = e.NewValue as ICommand;
                control.UpdateCommand(oldCommand, newCommand);
            }
        }
        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            base.OnMouseLeftButtonDown(e);
            RoutedCommand rm = Command as RoutedCommand;
            if (rm!=null)
            {
                rm.Execute(CommandParameter,CommandTarget);
            }
            else if (this.Command!=null)
            {
                this.Command.Execute(CommandParameter);
            }
        }
        private void UpdateCommand(ICommand oldCommand, ICommand newCommand)
        {
            if (oldCommand != null)
            {
                oldCommand.CanExecuteChanged -= CanExecuteChanged;
            }
            if (newCommand != null)
            {
                newCommand.CanExecuteChanged += CanExecuteChanged;
            }
        }

        private void CanExecuteChanged(object sender, EventArgs e)
        {
            RoutedCommand command = this.Command as RoutedCommand;
            if (command != null)
            {
                this.IsEnabled = command.CanExecute(CommandParameter, CommandTarget);
            }
            else if (this.Command != null)
            {
                this.IsEnabled = this.Command.CanExecute(CommandParameter);
            }
        }

        public ICommand Command
        {
            get
            {
                return (ICommand)GetValue(CommandProperty);
            }
            set
            {
                SetValue(CommandProperty, value);
            }
        }
        public object CommandParameter
        {
            get
            {
                return GetValue(CommandParameterProperty);
            }
            set
            {
                SetValue(CommandParameterProperty, value);
            }
        }
        public IInputElement CommandTarget
        {
            get
            {
                return (IInputElement)GetValue(CommandTargetProperty);
            }
            set
            {
                SetValue(CommandTargetProperty, value);
            }
        }
        #endregion

        public MyControl()
        {
            InitializeComponent();
            this.Loaded += MyControl_Loaded;
            this.txt1.PreviewTextInput += Txt1_PreviewTextInput; ;
        }
        private string lastinput = "";
        private void Txt1_PreviewTextInput(object sender, TextCompositionEventArgs e)
        {
            int ML = 0;
            int.TryParse(this.MaxLength, out ML);
            TextBox tb = sender as TextBox;
            if (ML > 0)
            {
                if (txt1.Text.Length > ML)
                {
                    tb.Text = lastinput;
                    tb.Select(tb.Text.Length, 0);
                    return;
                }
            }
            lastinput = tb.Text;
        }
       


        private void MyControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.txt1.Text = Pattern;
        }
    }
}
